<?php
class ExpenseController extends Zend_Controller_Action
{

	/**
	 * @var Cms_Model_Expense
	 */
	protected $_expenseModel;

	/**
	 * @var array
	 */
	protected $_forms = array();

	/**
	 * @var Zend_Controller_Action_Helper_Url
	 */
	protected $_urlHelper;

	public function init()
	{
		$this->_expenseModel = new Cms_Model_Expense();

		$this->_urlHelper = $this->_helper->getHelper('url');
	}
	
	
	public function expenseAction()
	{
		$action = $this->_getParam('do');
	
		switch($action){
	
			case 'insert':
	
				$this->view->backUrl = $this->_urlHelper->url(array('do'=>'access'),'expenseExpense', true);
	
				$this->view->expenseForm = $this->_getExpenseForm();
	
				$request = $this->getRequest();
	
				if($request->isPost()){
					if (!$this->_forms['expenseForm']->isValid($request->getPost())) {
						return $this->render('saveexpense');
					}
					$authService = new Cms_Service_Authentication();
					$this->_expenseModel->saveExpense($this->_forms['expenseForm']->getValues(), $authService->getIdentity()->fullname);
					$this->_helper->flashMessenger->setNamespace('returnMessageSuccess')->addMessage("Success: Expense has been successfully created!");
					$this->_helper->redirector->gotoRoute(array('do'=>'access'),'expenseExpense', true);
				}
	
				$this->_helper->viewRenderer('saveexpense');
				break;
	
			case 'update':
	
				$entryId = (int) $this->_getParam('id');
	
				$this->view->backUrl = (null !== $this->_helper->redirectUrl()) ? $this->_helper->redirectUrl() : $this->_urlHelper->url(array('do'=>'access'),'expenseExpense', true);
	
				//if expense id does not exists - redirect to expense access
				$expenseInfo = $this->_expenseModel->getExpenseByEntryId($entryId);
				if((null === $expenseInfo)){
					$this->_helper->redirector->gotoRoute(array('do'=>'access'),'expenseExpense', true);
				}
	
				$this->view->expenseForm = $this->_getExpenseForm('update', $entryId);
	
				$request = $this->getRequest();
	
				if($request->isPost()){
	
					if (!$this->_forms['expenseForm']->isValid($request->getPost())) {
						return $this->render('saveexpense');
					}
	
					$authService = new Cms_Service_Authentication();
					$this->_expenseModel->saveExpense($this->_forms['expenseForm']->getValues(), $authService->getIdentity()->fullname, $expenseInfo);
					$this->_helper->flashMessenger->setNamespace('returnMessageSuccess')->addMessage("Success: Expense has been successfully updated!");
	
					if(null !== $this->_helper->redirectUrl()){
						$this->_redirect($this->_helper->redirectUrl(), array('prependBase'=>false));
					}else{
						$this->_helper->redirector->gotoRoute(array('do'=>'access'),'expenseExpense', true);
					}
	
				}
	
				$expenseInfo = $expenseInfo->toArray();
				 
				$mysqlDate = new Zend_Date($expenseInfo['expenseDate'], 'yyyy.MM.dd');
				$expenseInfo['expenseDate'] = $mysqlDate->toString('dd/MM/yyyy');
				 
				$this->view->expenseForm->populate($expenseInfo);
	
				$this->_helper->viewRenderer('saveexpense');
	
				break;
	
			case 'delete':
	
				if($this->getRequest()->isPost()){
	
					$expenses = $this->_getParam('selected');
	
					if ((!empty($expenses) && (is_array($expenses)))) {
						foreach ($expenses as $expense) {
							//delete expense
							$this->_expenseModel->deleteExpense($this->_expenseModel->getExpenseByEntryId($expense));
						}
						$this->_helper->flashMessenger->setNamespace('returnMessageSuccess')->addMessage("Success: Expense(s) has been successfully deleted!");
					}
	
					if(null !== $this->_helper->redirectUrl()){
						$this->_redirect($this->_helper->redirectUrl(), array('prependBase'=>false));
					}else{
						$this->_helper->redirector->gotoRoute(array('do'=>'access'),'expenseExpense', true);
					}
	
				}else{
					$this->_helper->redirector->gotoRoute(array('do'=>'access'),'expenseExpense', true);
				}
	
				break;
	
			case 'access':
			default:
	
				if ($this->_helper->flashMessenger->setNamespace('returnMessageSuccess')->hasMessages()) {
					$this->view->returnMessageSuccess = $this->_helper->flashMessenger->setNamespace('returnMessageSuccess')->getMessages();
				}
	
				if ($this->_helper->flashMessenger->setNamespace('returnMessageWarning')->hasMessages()) {
					$this->view->returnMessageWarning = $this->_helper->flashMessenger->setNamespace('returnMessageWarning')->getMessages();
				}
	
				$data = array();
				$partHeadUrl = '';
				$partPaginationUrl = '';
	
				if ($this->_getParam('sort')) {
					$data['sort'] = $this->_getParam('sort');
					$partPaginationUrl .= '&sort=' . $this->_getParam('sort');
	
					if ($this->_getParam('order')) {
						$data['order'] = $this->_getParam('order');
						$partPaginationUrl .= '&order=' . $this->_getParam('order');
					}else{
						$data['order'] = 'asc';
						$partPaginationUrl .= '&order=asc';
					}
				}
	
				$partHeadUrl .= ($this->_getParam('order') == 'asc') ? '&order=desc' : '&order=asc';
	
				if ($this->_getParam('page')){
					$data['page'] = intval($this->_getParam('page'));
					$partHeadUrl .= '&page=' . intval($this->_getParam('page'));
				}else{ //turn on pagination by default
					$data['page'] = 1;
				}
				
				if ($this->_getParam('search')) {
					$data['search'] = $this->_getParam('search');
					$partHeadUrl .= '&search=' . $this->_getParam('search');
					$partPaginationUrl .= '&search=' . $this->_getParam('search');
				}
	
				$this->view->data = $data;
				$this->view->partHeadUrl = $partHeadUrl;
				$this->view->partPaginationUrl = $partPaginationUrl;
	
				$this->view->expenses = ($this->_expenseModel->getExpenses($data)->count() > 0) ? $this->_expenseModel->getExpenses($data) : null;
				$this->_helper->viewRenderer('listexpense');
				break;
		}
	}
	
	protected function _getExpenseForm($formState='create',$entryId=null)
	{
		$this->_forms['expenseForm'] = $this->_expenseModel->getForm('expenseExpenseForm');
		$this->_forms['expenseForm']->setMethod('post');
	
		if($formState == 'update'){
			$this->_forms['expenseForm']->setAction($this->_urlHelper->url(array('do'=>'update','id'=>$entryId), 'expenseExpense', true).$this->_helper->redirectUrl(true));
		}else{
			$this->_forms['expenseForm']->setAction($this->_urlHelper->url(array('do'=>'insert'), 'expenseExpense', true).$this->_helper->redirectUrl(true));
		}
	
		return $this->_forms['expenseForm'];
	}

}